library(tidyverse)
## ── Attaching packages ─────────────────────────────────────── tidyverse 1.3.1 ──
## ✓ ggplot2 3.3.5 ✓ purrr 0.3.4
## ✓ tibble 3.1.6 ✓ dplyr 1.0.8
## ✓ tidyr 1.2.0 ✓ stringr 1.4.0
## ✓ readr 2.1.2 ✓ forcats 0.5.1
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## x dplyr::filter() masks stats::filter()
## x dplyr::lag() masks stats::lag()
library(ggmap)
## Google's Terms of Service: https://cloud.google.com/maps-platform/terms/.
## Please cite ggmap if you use it! See citation("ggmap") for details.
library(here) # for specifying directory location
## here() starts at /Users/soltoffbc/Projects/Data Visualization/exercises/raster-maps
Import 311 service requests
chi_311 <- read_csv(file = here("data", "chicago-311.csv"))
## Rows: 193299 Columns: 7
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (2): sr_number, sr_short_code
## dbl (4): community_area, ward, latitude, longitude
## dttm (1): created_date
##
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
chi_311
## # A tibble: 193,299 × 7
## sr_number sr_short_code created_date community_area ward latitude
## <chr> <chr> <dttm> <dbl> <dbl> <dbl>
## 1 SR19-01209373 SGQ 2019-03-23 17:13:05 58 12 41.8
## 2 SR19-01129184 SGQ 2019-03-09 01:37:26 40 20 41.8
## 3 SR19-01130159 SGQ 2019-03-09 14:51:46 40 20 41.8
## 4 SR19-01142266 SGQ 2019-03-12 12:17:31 67 17 41.8
## 5 SR19-01142389 SGQ 2019-03-12 12:46:05 59 12 41.8
## 6 SR19-01142452 SGQ 2019-03-12 12:55:17 59 12 41.8
## 7 SR19-01137036 SGQ 2019-03-11 14:35:45 2 40 42.0
## 8 SR19-01142278 SGQ 2019-03-12 12:19:09 59 12 41.8
## 9 SR19-01142678 SGQ 2019-03-12 13:35:02 59 12 41.8
## 10 SR19-01142691 SGQ 2019-03-12 13:38:39 64 13 41.8
## # … with 193,289 more rows, and 1 more variable: longitude <dbl>
Obtain map tiles using ggmap for the city of
Chicago.
# store bounding box coordinates
chi_bb <- c(
left = -87.936287,
bottom = 41.679835,
right = -87.447052,
top = 42.000835
)
# retrieve bounding box
chicago <- get_stamenmap(
bbox = chi_bb,
zoom = 11
)
## Source : http://tile.stamen.com/terrain/11/523/760.png
## Source : http://tile.stamen.com/terrain/11/524/760.png
## Source : http://tile.stamen.com/terrain/11/525/760.png
## Source : http://tile.stamen.com/terrain/11/526/760.png
## Source : http://tile.stamen.com/terrain/11/523/761.png
## Source : http://tile.stamen.com/terrain/11/524/761.png
## Source : http://tile.stamen.com/terrain/11/525/761.png
## Source : http://tile.stamen.com/terrain/11/526/761.png
## Source : http://tile.stamen.com/terrain/11/523/762.png
## Source : http://tile.stamen.com/terrain/11/524/762.png
## Source : http://tile.stamen.com/terrain/11/525/762.png
## Source : http://tile.stamen.com/terrain/11/526/762.png
# plot the raster map
ggmap(chicago)

Generate a scatterplot of complaints about potholes in streets.
# initialize map
ggmap(chicago) +
# add layer with scatterplot
# use alpha to show density of points
geom_point(
data = filter(chi_311, sr_short_code == "PHF"),
mapping = aes(
x = longitude,
y = latitude
),
size = .25,
alpha = .05
)
## Warning: Removed 8863 rows containing missing values (geom_point).

Generate a heatmap of complaints about potholes in streets. Do you
see any unusual patterns or clusterings?
# initialize the map
ggmap(chicago) +
# add the heatmap
stat_density_2d(
data = filter(chi_311, sr_short_code == "PHF"),
mapping = aes(
x = longitude,
y = latitude,
fill = stat(level)
),
alpha = .1,
bins = 50,
geom = "polygon"
)
## Warning: Removed 8863 rows containing non-finite values (stat_density2d).

Obtain map tiles for Hyde Park using the toner map tiles
# store bounding box coordinates
hp_bb <- c(
left = -87.608221,
bottom = 41.783249,
right = -87.577643,
top = 41.803038
)
# retrieve bounding box
hyde_park <- get_stamenmap(
bbox = hp_bb,
zoom = 15,
maptype = "toner"
)
## Source : http://tile.stamen.com/toner/15/8409/12188.png
## Source : http://tile.stamen.com/toner/15/8410/12188.png
## Source : http://tile.stamen.com/toner/15/8411/12188.png
## Source : http://tile.stamen.com/toner/15/8412/12188.png
## Source : http://tile.stamen.com/toner/15/8409/12189.png
## Source : http://tile.stamen.com/toner/15/8410/12189.png
## Source : http://tile.stamen.com/toner/15/8411/12189.png
## Source : http://tile.stamen.com/toner/15/8412/12189.png
## Source : http://tile.stamen.com/toner/15/8409/12190.png
## Source : http://tile.stamen.com/toner/15/8410/12190.png
## Source : http://tile.stamen.com/toner/15/8411/12190.png
## Source : http://tile.stamen.com/toner/15/8412/12190.png
# plot the raster map
ggmap(hyde_park)

Generate a scatterplot of requests to pick up dead animals in Hyde
Park
# initialize the map
ggmap(hyde_park) +
# add a scatterplot layer
geom_point(
data = filter(chi_311, sr_short_code == "SGQ"),
mapping = aes(
x = longitude,
y = latitude
)
)
## Warning: Removed 34287 rows containing missing values (geom_point).

Session Info
sessioninfo::session_info()
## ─ Session info ───────────────────────────────────────────────────────────────
## setting value
## version R version 4.1.2 (2021-11-01)
## os macOS Monterey 12.2.1
## system aarch64, darwin20
## ui X11
## language (EN)
## collate en_US.UTF-8
## ctype en_US.UTF-8
## tz America/Chicago
## date 2022-05-02
## pandoc 2.17.1.1 @ /Applications/RStudio.app/Contents/MacOS/quarto/bin/ (via rmarkdown)
##
## ─ Packages ───────────────────────────────────────────────────────────────────
## package * version date (UTC) lib source
## assertthat 0.2.1 2019-03-21 [1] CRAN (R 4.1.0)
## backports 1.4.1 2021-12-13 [1] CRAN (R 4.1.1)
## bit 4.0.4 2020-08-04 [1] CRAN (R 4.1.1)
## bit64 4.0.5 2020-08-30 [1] CRAN (R 4.1.0)
## bitops 1.0-7 2021-04-24 [1] CRAN (R 4.1.0)
## broom 0.7.12 2022-01-28 [1] CRAN (R 4.1.1)
## bslib 0.3.1 2021-10-06 [1] CRAN (R 4.1.1)
## cellranger 1.1.0 2016-07-27 [1] CRAN (R 4.1.0)
## cli 3.2.0 2022-02-14 [1] CRAN (R 4.1.1)
## colorspace 2.0-3 2022-02-21 [1] CRAN (R 4.1.1)
## crayon 1.5.1 2022-03-26 [1] CRAN (R 4.1.2)
## curl 4.3.2 2021-06-23 [1] CRAN (R 4.1.0)
## DBI 1.1.2 2021-12-20 [1] CRAN (R 4.1.1)
## dbplyr 2.1.1 2021-04-06 [1] CRAN (R 4.1.0)
## digest 0.6.29 2021-12-01 [1] CRAN (R 4.1.1)
## dplyr * 1.0.8 2022-02-08 [1] CRAN (R 4.1.1)
## ellipsis 0.3.2 2021-04-29 [1] CRAN (R 4.1.0)
## evaluate 0.15 2022-02-18 [1] CRAN (R 4.1.1)
## fansi 1.0.3 2022-03-24 [1] CRAN (R 4.1.1)
## farver 2.1.0 2021-02-28 [1] CRAN (R 4.1.0)
## fastmap 1.1.0 2021-01-25 [1] CRAN (R 4.1.0)
## forcats * 0.5.1 2021-01-27 [1] CRAN (R 4.1.1)
## fs 1.5.2 2021-12-08 [1] CRAN (R 4.1.1)
## generics 0.1.2 2022-01-31 [1] CRAN (R 4.1.1)
## ggmap * 3.0.0 2019-02-05 [1] CRAN (R 4.1.1)
## ggplot2 * 3.3.5 2021-06-25 [1] CRAN (R 4.1.1)
## glue 1.6.2 2022-02-24 [1] CRAN (R 4.1.1)
## gtable 0.3.0 2019-03-25 [1] CRAN (R 4.1.1)
## haven 2.4.3 2021-08-04 [1] CRAN (R 4.1.1)
## here * 1.0.1 2020-12-13 [1] CRAN (R 4.1.0)
## highr 0.9 2021-04-16 [1] CRAN (R 4.1.0)
## hms 1.1.1 2021-09-26 [1] CRAN (R 4.1.1)
## htmltools 0.5.2 2021-08-25 [1] CRAN (R 4.1.1)
## httr 1.4.2 2020-07-20 [1] CRAN (R 4.1.0)
## isoband 0.2.5 2021-07-13 [1] CRAN (R 4.1.0)
## jpeg 0.1-9 2021-07-24 [1] CRAN (R 4.1.0)
## jquerylib 0.1.4 2021-04-26 [1] CRAN (R 4.1.0)
## jsonlite 1.8.0 2022-02-22 [1] CRAN (R 4.1.1)
## knitr 1.38 2022-03-25 [1] CRAN (R 4.1.1)
## labeling 0.4.2 2020-10-20 [1] CRAN (R 4.1.0)
## lattice 0.20-45 2021-09-22 [1] CRAN (R 4.1.2)
## lifecycle 1.0.1 2021-09-24 [1] CRAN (R 4.1.1)
## lubridate 1.8.0 2021-10-07 [1] CRAN (R 4.1.1)
## magrittr 2.0.3 2022-03-30 [1] CRAN (R 4.1.1)
## MASS 7.3-56 2022-03-23 [1] CRAN (R 4.1.1)
## modelr 0.1.8 2020-05-19 [1] CRAN (R 4.1.0)
## munsell 0.5.0 2018-06-12 [1] CRAN (R 4.1.0)
## pillar 1.7.0 2022-02-01 [1] CRAN (R 4.1.1)
## pkgconfig 2.0.3 2019-09-22 [1] CRAN (R 4.1.0)
## plyr 1.8.7 2022-03-24 [1] CRAN (R 4.1.1)
## png 0.1-7 2013-12-03 [1] CRAN (R 4.1.0)
## purrr * 0.3.4 2020-04-17 [1] CRAN (R 4.1.0)
## R6 2.5.1 2021-08-19 [1] CRAN (R 4.1.1)
## Rcpp 1.0.8.3 2022-03-17 [1] CRAN (R 4.1.1)
## readr * 2.1.2 2022-01-30 [1] CRAN (R 4.1.1)
## readxl 1.3.1 2019-03-13 [1] CRAN (R 4.1.0)
## reprex 2.0.1 2021-08-05 [1] CRAN (R 4.1.1)
## RgoogleMaps 1.4.5.3 2020-02-12 [1] CRAN (R 4.1.0)
## rjson 0.2.21 2022-01-09 [1] CRAN (R 4.1.1)
## rlang 1.0.2 2022-03-04 [1] CRAN (R 4.1.1)
## rmarkdown 2.13 2022-03-10 [1] CRAN (R 4.1.1)
## rprojroot 2.0.2 2020-11-15 [1] CRAN (R 4.1.0)
## rstudioapi 0.13 2020-11-12 [1] CRAN (R 4.1.0)
## rvest 1.0.2 2021-10-16 [1] CRAN (R 4.1.1)
## sass 0.4.1 2022-03-23 [1] CRAN (R 4.1.1)
## scales 1.2.0 2022-04-13 [1] CRAN (R 4.1.2)
## sessioninfo 1.2.2 2021-12-06 [1] CRAN (R 4.1.1)
## sp 1.4-6 2021-11-14 [1] CRAN (R 4.1.1)
## stringi 1.7.6 2021-11-29 [1] CRAN (R 4.1.1)
## stringr * 1.4.0 2019-02-10 [1] CRAN (R 4.1.1)
## tibble * 3.1.6 2021-11-07 [1] CRAN (R 4.1.1)
## tidyr * 1.2.0 2022-02-01 [1] CRAN (R 4.1.1)
## tidyselect 1.1.2 2022-02-21 [1] CRAN (R 4.1.1)
## tidyverse * 1.3.1 2021-04-15 [1] CRAN (R 4.1.0)
## tzdb 0.2.0 2021-10-27 [1] CRAN (R 4.1.1)
## utf8 1.2.2 2021-07-24 [1] CRAN (R 4.1.0)
## vctrs 0.4.0 2022-03-30 [1] CRAN (R 4.1.1)
## vroom 1.5.7 2021-11-30 [1] CRAN (R 4.1.1)
## withr 2.5.0 2022-03-03 [1] CRAN (R 4.1.1)
## xfun 0.30 2022-03-02 [1] CRAN (R 4.1.1)
## xml2 1.3.3 2021-11-30 [1] CRAN (R 4.1.1)
## yaml 2.3.5 2022-02-21 [1] CRAN (R 4.1.1)
##
## [1] /Library/Frameworks/R.framework/Versions/4.1-arm64/Resources/library
##
## ──────────────────────────────────────────────────────────────────────────────